package net.wsck;

import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.rmi.ConnectIOException;
import java.util.Date;
import java.util.logging.Logger;

import org.w3c.dom.ranges.RangeException;

import net.telnet.SocketClient;

import starp.Main;
import sun.nio.ch.SocketAdaptor;
import interf.ITelnetHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;

/**
 * 客户端对象
 * @author fang
 * 2017-2-20上午12:46:38
 */
public class MudClient implements ITelnetHandler{


	private static final Logger logger = Logger.getLogger(MudClient.class.getName());
	public int linkMudPort;
	public ChannelHandlerContext ctx;
	/**
	 * 和MUD连接的SOCKET
	 */
	public SocketClient mudSock;
	private String svrIp;
	private TextWebSocketFrame toData;
	
	public String getSvrIp() {
		return svrIp;
	}


	public void setSvrIp(String svrIp) {
		this.svrIp = svrIp;
	}

	private int port;
	
	
	public int getPort() {
		return port;
	}


	public ChannelHandlerContext getCtx() {
		return ctx;
	}


	public void setCtx(ChannelHandlerContext ctx) {
		this.ctx = ctx;
	}


	public void setPort(int port) {
		this.port = port;
	}


	public MudClient(ChannelHandlerContext ctxhand){
		ctx = ctxhand;
	}
	
	/**
	 * 关闭MUD的连接
	 * @项目：FFOS
	 * @方法描述:
	 * @参数:
	 * @返回值：
	 * @作者: mike@大当家@QQ184377367
	 * @日期: 2018-6-16上午01:26:34
	 */
	private void closeMudServerConnect(){
		this.mudSock.stop();
	}
	/**
	 * 进行销毁
	 * @项目：FFOS
	 * @方法描述:
	 * @参数:
	 * @返回值：
	 * @作者: mike@大当家@QQ184377367
	 * @日期: 2018-6-16上午01:27:02
	 */
	public void free(){
		this.closeMudServerConnect();
	}
	
	

	/**
	 * 绑定到MUD服务端
	 * @项目：FFOS
	 * @方法描述:
	 * @参数:
	 * @返回值：
	 * @作者: mike@大当家@QQ184377367
	 * @日期: 2018-6-15下午11:54:08
	 */
	public void bindMudServer() {
		mudSock = new SocketClient( this );
		mudSock.host = this.getSvrIp();
		mudSock.port = this.getPort();
		mudSock.start();

	}


	
	/**
	 * WEB客户端消息
	 */
	public void onWebClinetMessage(String s) {
		
		
		if( s.startsWith("100010") ){
			//客户端发来要连接的MUD服务端
			String sAry[];
			logger.info(String.format("客户端需要连接%s", s));
			sAry = s.split(":");
			this.setSvrIp(sAry[1]);
			this.setPort( Integer.parseInt(sAry[2]) );
			this.bindMudServer();
//			
//			if(mudSock.getTelnet()==null || !mudSock.getTelnet().isConnected()){
//				toData =  new TextWebSocketFrame("指定的MUD服务器连接失败，请重新连接！");
//				ctx.writeAndFlush(toData);
//			}
		
			return;
		}
		
		if( s.startsWith("100009") ){
			toData =  new TextWebSocketFrame("100010");
			ctx.writeAndFlush(toData);
			logger.info("通知客户端发送需要连接的MUD服务器");
			return;
		}
		
		
		
		if( this.getSvrIp()==null || this.getSvrIp().equals("") || this.getPort()==0 ){
			
			//没有收到客户端发来的需要连接的MUD服务器和端口 
			toData =  new TextWebSocketFrame("100010");
			ctx.writeAndFlush(toData);
			logger.info("等待客户端发送需要连接服务器");
			return ;
		}
		

		
		mudSock.sendMsg(s);
		
	}
	
	
	
	public void OnRecvComplete(String s) {
		// TODO Auto-generated method stub
		toData =  new TextWebSocketFrame(s);
		
		
		ctx.writeAndFlush(toData);
		
	}
}
